﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public class SimpleSaplItem : ItemFlat, Interface.GrowableSapl
    {
        public string TextureName;

        public virtual int StateDuration => 1200;

        public virtual int MaxState => 1;
        
        public virtual int WoodIndex { get; set; }
        
        public virtual int LeavesIndex { get; set; }

        public virtual int SoilValue { get; set; } = GrassBlock.Index;//草地

        public virtual int WitheredValue { get; set; } = DryBushBlock.Index;//干灌木

        public SimpleSaplItem( string n, string name) : base( n, ContentManager.Get<Texture2D>("Mekiasm/Textures/sapling/" + name))
        {
            TextureName = name;
        }
        public override int GetTextureSlotCount(int value)
        {
            return 1;
        }

        public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
        {
            dropValues.Add(new BlockDropValue() { Value = blockValue, Count = 1 });
            showDebris = true;
        }

        public override bool IsFaceTransparent(SubsystemTerrain subsystemTerrain, int face, int value)
        {
            return true;
        }
        public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, MovingBlock value)
        {
            return new BoundingBox[0];
        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }
        
        public override bool IsPlaceable_(int value)
        {
            return true;
        }

        public override void GenerateTerrainVertices(Block block, BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            generator.GenerateCrossfaceVertices(block, value, x, y, z, Color.White, 0, geometry.GetGeometry(texture2D).SubsetAlphaTest);
        }

        public override PlantSystem.PlantItem GetPlantItem(int value, int x, int y, int z)
        {
            return new PlantSystem.PlantItem(x, y, z, blockValue, StateDuration, (item, bottomID, hydration, nitrogen) =>
            {                
                if (bottomID != SoilValue)
                {
                    item.Remove = true;
                    ILibrary.SubsystemTerrain.ChangeCell(x, y, z, WitheredValue);
                    return;
                }
                if (item.GrowState == MaxState)
                {
                    MekPlantsManager mekPlantsManager = new MekPlantsManager(WoodIndex, LeavesIndex);
                    mekPlantsManager.Paint(ILibrary.SubsystemTerrain, x, y + 1, z);
                    item.Remove = true;
                }
            }, (v) =>
            {
                if (ILibrary.GetBasicValue(v) != blockValue && ILibrary.GetBasicValue(v) != WoodIndex)
                {
                    return false;
                }
                return true;
            });
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            PlantSystem.Add(GetPlantItem(value, x, y, z));

        }
    }
}
